package org.guiceside.commons.lang;

import java.util.Date;

/**
 * @author zhenjia <a href='mailto:zhenjiaWang@gmail.com'>email</a>
 * @since JDK1.5
 * @version 1.0 2008-10-14
 * 
 **/
public class StringUtils extends org.apache.commons.lang.StringUtils {
	private final static String nullStr = "";

	private StringUtils() {

	}

    /**
     * 将连续的多余空格清除
     * @param str
     * @return
     */
    public static String clearSpilthBank(String str){
        if(isBlank(str)){
            return null;
        }
        char[] chars=str.toCharArray();
        StringBuilder stringBuilder=new StringBuilder();
        boolean bankExist=false;
        if(chars!=null && chars.length>0){
            for(char c:chars){
                int asci=(int)c;
                if(asci==32){
                    if(!bankExist){
                        bankExist=true;
                        stringBuilder.append(c);
                    }else{
                        continue;
                    }
                }else{
                    bankExist=false;
                    stringBuilder.append(c);
                }
            }
        }
        return stringBuilder.toString();
    }
    public static boolean getYesNo(String str){
        if(StringUtils.isNotBlank(str)){
            if(str.toLowerCase().equals("true")){
                return true;
            }else if(str.toLowerCase().equals("false")){
                return false;
            }else {
                return false;
            }
        }else{
            return false;
        }
    }
	public static String toUtf8String(String s) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < s.length(); i++) {
			char c = s.charAt(i);
			if (c >= 0 && c <= 255) {
				sb.append(c);
			} else {
				byte[] b;
				try {
					b = Character.toString(c).getBytes("utf-8");
				} catch (Exception ex) {
					b = new byte[0];
				}
				for (int j = 0; j < b.length; j++) {
					int k = b[j];
					if (k < 0)
						k += 256;
					sb.append("%" + Integer.toHexString(k).toUpperCase());
				}
			}
		}
		String s_utf8 = sb.toString();
		sb.delete(0, sb.length());
		sb.setLength(0);
		sb = null;
		return s_utf8;
	}

	public static String defaultIfEmptyByDate(Date source, String pattern) {
		if (source == null) {
			return nullStr;
		}
		return DateFormatUtil.format(source, pattern);
	}

	public static String defaultIfEmpty(String source) {
		return org.apache.commons.lang.StringUtils.defaultIfEmpty(source,
				nullStr);
	}

	public static String defaultIfEmpty(String source, String nullSTR) {
		return org.apache.commons.lang.StringUtils.defaultIfEmpty(source,
				nullSTR);
	}

	public static String defaultIfEmpty(Object source) {
		if (source == null) {
			return nullStr;
		}
		return org.apache.commons.lang.StringUtils.defaultIfEmpty(source
				.toString(), nullStr);
	}

	public static String defaultIfEmpty(Object source, String nullSTR) {
		if (source == null) {
			return nullSTR;
		}
		return org.apache.commons.lang.StringUtils.defaultIfEmpty(source
				.toString(), nullSTR);
	}

	public static String limitLength(String content, int length) {
		if (StringUtils.isBlank(content)) {
			return "";
		}
		if (content.length() >= length) {
			content = content.substring(0, length) + "..";
		}
		return content;
	}

}
